{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "981a55df-293f-4959-a537-8e1cd21cbcce",
   "metadata": {},
   "outputs": [],
   "source": [
    "import zhipuai\n",
    "import jsonlines\n",
    "import json\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44943e44-de0c-4646-8664-f363419e015f",
   "metadata": {},
   "outputs": [],
   "source": [
    "zhipuai.api_key = \"your zhipu_ai api key\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41b31c6c-42f0-4b89-91c3-6bbc89b2fe0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = list(jsonlines.open(\"../data/wiki_roleplay_multilingual_test_input_w_evidence.jsonl\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c8c8892-d337-4ae2-9ee5-863cf69d40f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "results = []\n",
    "for each in tqdm(data):\n",
    "    message = []\n",
    "    for prompt in each['prompts']:\n",
    "        message.append({\"role\": \"user\", \"content\": prompt})\n",
    "        response = zhipuai.model_api.invoke(\n",
    "            model=\"characterglm\",\n",
    "           meta= {\n",
    "                \"user_info\": \"用户\",\n",
    "                \"user_name\": \"用户\",\n",
    "                \"bot_info\": each['system'],\n",
    "                \"bot_name\": each['meta']['label'],\n",
    "            },\n",
    "            prompt = message\n",
    "        )\n",
    "        if not response['success']:\n",
    "            message.pop(-1)\n",
    "            continue\n",
    "        message.append({\"role\": \"assistant\", \"content\": json.loads(response['data']['choices'][0]['content']).strip()})\n",
    "    each['messages'] = message\n",
    "    results.append(each)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cbd4d268-2b5d-4ce2-b031-a6728cb66dad",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(len(results)):\n",
    "    results[i]['messages'] = [{'role': 'system', 'content': results[i]['system']}] + results[i]['messages']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f53ea096-554a-481b-9f2c-f62f97de116f",
   "metadata": {},
   "outputs": [],
   "source": [
    "with jsonlines.open(f\"../data/results/wiki_roleplay_multilingual_test_input_w_evidence_characterglm_output.jsonl\", \"w\") as f:\n",
    "    for each in results:\n",
    "        f.write(each)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
